package everydayGrade.t6;

/**
 * @Author: Siantar
 * @Date: 2023-12-01-12:23
 * @Description: 1.0
 */
public class T2661 {
    public int firstCompleteIndex(int[] arr, int[][] mat) {
        int m = mat.length, n = mat[0].length;
        int[][] rc = new int[m * n + 1][2];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                int num = mat[i][j];
                rc[num][0] = i;
                rc[num][1] = j;
            }
        }
        int[] row = new int[m];
        int[] col = new int[n];
        for (int i = 0; i < arr.length; i++) {
            int num = arr[i];
            int x = rc[num][0], y = rc[num][1];
            row[x]++;
            col[y]++;
            if (row[x] == n || col[y] == m) {
                return i;
            }
        }
        return -1;
    }
}
